package com.example.newbee.mvp.model;

import com.example.mylibrary.http.RetrofitManager;
import com.example.mylibrary.mvp.model.BaseModel;
import com.example.newbee.api.APi;
import com.example.newbee.bean.UserBean;
import com.example.newbee.mvp.contact.RegisterContact;
import com.google.gson.JsonObject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.http.Body;

public class RegisterModel extends BaseModel implements RegisterContact.Model {

    @Override
    public void login(String username, String pwd, Observer<UserBean> observer) {
        RetrofitManager.getInstance().getRetrofit().create(APi.class)
                .login(username,pwd)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(observer);
    }

    @Override
    public void register(String username, String pwd, Observer<UserBean> observer) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("username", username);
        jsonObject.addProperty("user_password", pwd);
        jsonObject.addProperty("userlevel", 1);
        jsonObject.addProperty("uservip", 1);

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
        RetrofitManager.getInstance().getRetrofit().create(APi.class)
                .register(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(observer);


    }


}
